package com.ruoyi.lxzx.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.lxzx.appVO.lxqlVO.GroupVO;
import com.ruoyi.lxzx.domain.UserCircle;
import com.ruoyi.lxzx.mapper.GroupMemberMapper;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lxzx.mapper.GroupMapper;
import com.ruoyi.lxzx.domain.Group;
import com.ruoyi.lxzx.service.IGroupService;

/**
 * 老乡圈Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-26
 */
@Service
@AllArgsConstructor
public class GroupServiceImpl implements IGroupService 
{
    private final GroupMapper groupMapper;
    private final GroupMemberMapper groupMemberMapper;

    /**
     * 查询老乡圈
     * 
     * @param id 老乡圈主键
     * @return 老乡圈
     */
    @Override
    public Group selectGroupById(Long id)
    {
        return groupMapper.selectGroupById(id);
    }

    /**
     * 查询老乡圈列表
     * 
     * @param group 老乡圈
     * @return 老乡圈
     */
    @Override
    public List<Group> selectGroupList(Group group)
    {
        return groupMapper.selectGroupList(group);
    }

    /**
     * 新增老乡圈
     * 
     * @param group 老乡圈
     * @return 结果
     */
    @Override
    public int insertGroup(Group group)
    {
        Long finalUserId = 0L;
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        group.setUserId(finalUserId);
        group.setStatus("1");
        group.setCreateBy(finalUserId+"");
        group.setCreateTime(DateUtils.getNowDate());
        return groupMapper.insertGroup(group);
    }

    /**
     * 修改老乡圈
     * 
     * @param group 老乡圈
     * @return 结果
     */
    @Override
    public int updateGroup(Group group)
    {
        group.setUpdateTime(DateUtils.getNowDate());
        return groupMapper.updateGroup(group);
    }

    /**
     * 批量删除老乡圈
     * 
     * @param ids 需要删除的老乡圈主键
     * @return 结果
     */
    @Override
    public int deleteGroupByIds(Long[] ids)
    {
        return groupMapper.deleteGroupByIds(ids);
    }

    /**
     * 删除老乡圈信息
     * 
     * @param id 老乡圈主键
     * @return 结果
     */
    @Override
    public int deleteGroupById(Long id)
    {
        return groupMapper.deleteGroupById(id);
    }

    @Override
    public List<GroupVO> qlb() {
        List<Group> result = groupMapper.selectGroupList(null);
        List<GroupVO> vos = new ArrayList<>();
        for (Group group : result) {
            GroupVO g = new GroupVO();
            BeanUtils.copyProperties(group, g);
            List<String> imgList = groupMemberMapper.findImgByGroupId(g.getId());
            g.setCount(imgList.size());
            g.setImgList(imgList);
            vos.add(g);
        }
        return vos;
    }

    @Override
    public List<GroupVO> wdq(Long userId) {
        Group param = new Group();
        param.setUserId(userId);
        List<Group> result = groupMapper.selectGroupList(param);
        List<GroupVO> vos = new ArrayList<>();
        for (Group group : result) {
            GroupVO g = new GroupVO();
            BeanUtils.copyProperties(group, g);
            List<String> imgList = groupMemberMapper.findImgByGroupId(g.getId());
            g.setCount(imgList.size());
            g.setImgList(imgList);
            vos.add(g);
        }
        return vos;
    }

    @Override
    public void scq(Long groupId) {
        Long finalUserId = 0L;
        Long[] adminArr = SecurityUtils.getAdminIds();
        try {
            finalUserId = SecurityUtils.getUserId();
        } catch (Exception e) {
            throw new ServiceException("当前未登录,或登录已过期");
        }
        Group g = selectGroupById(groupId);
        Long finalUserId1 = finalUserId;
        Optional<Long> result = Arrays.stream(adminArr).filter(num -> num == finalUserId1).findFirst();

        if(g == null){
            throw new ServiceException("没有对应的群信息");
        } else if (!g.getUserId().equals(finalUserId) && !result.isPresent()) {
            throw new ServiceException("对应的群不是你所创建,不能删除");
        }else {
            groupMapper.deleteGroupById(groupId);
        }
    }
}
